package com.hx.service.impl;

import com.hx.dto.SysUserDTO;
import com.hx.mapper.SysUserMapper;
import com.hx.pojo.SysUser;
import com.hx.service.SysUserService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import javax.annotation.Resource;
import java.util.ArrayList;

import java.util.List;
import java.util.Objects;

@Service
@Slf4j
public class SysUserServiceImpl implements SysUserService {

    @Resource
    private SysUserMapper sysUserMapper;

    @Override
    public List<SysUser> findAllUser() {
        log.info("正在查询用户信息表...");
        return sysUserMapper.findAllUser();
    }

    /**
     * 将后端User用户变成前端展示UserDTO对象
     * @param sysUser
     * @return
     */
    @Override
    public SysUserDTO convertUserToUserDTO(SysUser sysUser) {

        if (Objects.isNull(sysUser)){
            log.error("输入用户信息为空!");
            throw new RuntimeException("用户输入信息为空!");
        }

        SysUserDTO sysUserDTO = new SysUserDTO();
        sysUserDTO.setId(sysUser.getId());
        sysUserDTO.setUid(String.valueOf(sysUser.getId()));
        sysUserDTO.setName(sysUser.getName());
        sysUserDTO.setNo(sysUser.getNo());
        sysUserDTO.setPosition(sysUser.getPosition());

        if ("1".equals(sysUser.getStatus())){
            sysUserDTO.setStatus("在职");
        }

        if ("2".equals(sysUser.getStatus())){
            sysUserDTO.setStatus("已离职");
        }
        sysUserDTO.setReason(sysUser.getReason());
        return sysUserDTO;
    }

    /**
     * 将后端User用户变成前端展示UserDTO对象
     * @param sysUserList
     * @return
     */
    @Override
    public List<SysUserDTO> convertUserToUserListDTO(List<SysUser> sysUserList) {

        if (CollectionUtils.isEmpty(sysUserList)){
            log.error("输入用户信息为空!");
            throw new RuntimeException("用户输入信息为空!");
        }

        List<SysUserDTO> userDTOList = new ArrayList<>();
        for (SysUser sysUser : sysUserList) {
            userDTOList.add(convertUserToUserDTO(sysUser));
        }
        return userDTOList;
    }

    /**
     * 保存用户信息
     * @param sysUser
     * @return
     */
    @Override
    public boolean saveUser(SysUserDTO sysUser) {
        sysUser.setStatus("在职");
        sysUserMapper.saveUser(convertUserDTOToUser(sysUser));
        log.info("成功保存用户信息...");
        return true;
    }

    /**
     * 将前端DTO信息转化为数据库User信息
     * @param sysUser
     * @return
     */
    private SysUser convertUserDTOToUser(SysUserDTO sysUser) {

        SysUser sysUserInfo = new SysUser();
        if (sysUser.getUid() != null){
            sysUserInfo.setId(Integer.valueOf(sysUser.getUid()));
        }else{
            sysUserInfo.setId(sysUser.getId());
        }

        sysUserInfo.setName(sysUser.getName());
        sysUserInfo.setNo(sysUser.getNo());
        sysUserInfo.setPosition(sysUser.getPosition());

        if (sysUser.getStatus().equals("在职") || sysUser.getStatus().equals("入职")){
            sysUserInfo.setStatus("1");
        }

        if (sysUser.getStatus().equals("已离职") || sysUser.getStatus().equals("离职")){
            sysUserInfo.setStatus("2");
        }
        sysUserInfo.setReason(sysUser.getReason());


        log.info("成功将UserDTO对象转化为User对象...");
        return sysUserInfo;
    }

    /**
     * 根据用户id查询用户信息
     * @param id
     * @return
     */
    @Override
    public SysUser findUserById(Integer id) {
        log.info("正在查询用户{}的信息",id);
        return sysUserMapper.findUserById(id);
    }

    /**
     * 更新用户信息
     * @param user
     * @return
     */
    @Override
    public boolean updateUserInfo(SysUserDTO user) {
        sysUserMapper.updateUserInfo(convertUserDTOToUser(user));
        log.info("正在更新用户信息...");
        return true;
    }

    /**
     * 更新用户状态
     * @param sysUserDTO
     * @return
     */
    @Override
    public boolean updateUserStatus(SysUserDTO sysUserDTO) {

        sysUserMapper.updateUserStatus(convertUserDTOToUser(sysUserDTO));
        log.info("正在更新用户状态...");
        return true;
    }
}
